package com.last1mile.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.last1mile.commons.ReserveStatusEnum;
import com.last1mile.dao.interfaces.ServeReserveDao;
import com.last1mile.model.Reserve;
import com.last1mile.pojo.ServeReserveDO;
import com.last1mile.service.ReserveService;

@Component("reserveServiceImpl")
public class ReserveServiceImpl implements ReserveService {
    @Autowired
    @Qualifier("serveReserveDao")
    private ServeReserveDao  serveReserveDao;

    @Override
    public Integer createReserve(Reserve reserve) {
        ServeReserveDO serveReserveDo = new ServeReserveDO();
        serveReserveDo.setMerId(reserve.getMerId());
        serveReserveDo.setMerServeId(reserve.getMerServeId());
        serveReserveDo.setPerId(reserve.getPerId());
        serveReserveDo.setBeginTime(reserve.getBeginTime());
        serveReserveDo.setEndTime(reserve.getEndTime());
        serveReserveDo.setNote(reserve.getNote());
        serveReserveDo.setStatus(ReserveStatusEnum.INIT.getValue());
        serveReserveDo.setAllDay(reserve.getAllDay());
        return this.serveReserveDao.insertServeReserve(serveReserveDo);
    }
    
    
    
    @Override
    public Integer updateReserveBeginEndTime(Integer reserveId,Date beginTime,Date endTime,Boolean allDay) {
        ServeReserveDO serveReserveDo = new ServeReserveDO();
        serveReserveDo.setId(reserveId);
        serveReserveDo.setBeginTime(beginTime);
        serveReserveDo.setEndTime(endTime);
        
        if(allDay != null){
            serveReserveDo.setAllDay(allDay);
        }
        
        return this.serveReserveDao.updateServeReserveBeginEndTime(serveReserveDo);
    }

    @Override
    public List<ServeReserveDO> queryServeReserve(Integer serveId, Date startDate, Date finishDate) {
        return this.queryReserve(null,serveId,null, startDate, finishDate,null);
    }
    
    @Override
    public Integer countServeReserve(Integer serveId) {
        return this.countReserve(null,serveId, null, null, null,null);
    }

    @Override
    public List<ServeReserveDO> queryMerReserve(Integer merId, Integer serveId, Date startDate, Date finishDate) {
        return this.queryReserve(merId, serveId,null, startDate, finishDate, null);
    }
    
    @Override
    public List<ServeReserveDO> queryPerReserve(Integer serveId,  Integer perId, Date startDate, Date finishDate) {
        return this.queryReserve(null, serveId, perId, startDate, finishDate,null);
    }

    
    private List<ServeReserveDO> queryReserve(Integer merId, Integer serveId,Integer perId,Date startDate,Date finishDate,String reserveStatus) {
        if(serveId != null){
            ServeReserveDO serveReserveDo = new ServeReserveDO();
            if(merId != null){
                serveReserveDo.setMerId(merId);
            }
            if(serveId != null){
            	serveReserveDo.setMerServeId(serveId);
            }
            
            if(perId != null){
                serveReserveDo.setPerId(perId);
            }
            
            if(startDate != null){
                serveReserveDo.setBeginTime(startDate);
            }
            
            if(finishDate != null){
                serveReserveDo.setEndTime(finishDate);
            }
            
            if(StringUtils.isNotBlank(reserveStatus)){
                serveReserveDo.setStatus(reserveStatus);
            }
            
            return this.serveReserveDao.queryServeReserve(serveReserveDo);
        }
        return null;
    }
    
    
    private Integer countReserve(Integer merId, Integer serveId,Integer perId,Date startDate,Date finishDate,String reserveStatus) {
        if(serveId != null){
        	ServeReserveDO serveReserveDo = new ServeReserveDO();
            if(merId != null){
                serveReserveDo.setMerId(merId);
            }
            if(serveId != null){
            	serveReserveDo.setMerServeId(serveId);
            }
            
            if(perId != null){
                serveReserveDo.setPerId(perId);
            }
            
            if(startDate != null){
                serveReserveDo.setBeginTime(startDate);
            }
            
            if(finishDate != null){
                serveReserveDo.setEndTime(finishDate);
            }
            
            if(StringUtils.isNotBlank(reserveStatus)){
                serveReserveDo.setStatus(reserveStatus);
            }
            
            return this.serveReserveDao.countServeReserve(serveReserveDo);
        }
        return 0;
    }
    
    @Override
    public ServeReserveDO queryReserveById(Integer id) {
        return this.serveReserveDao.queryServeReserve(id);
    }

    @Override
    public void updateReserveStatus(Integer id, ReserveStatusEnum reserveStatusEnum) {
        ServeReserveDO serveReserveDo = new ServeReserveDO();
        serveReserveDo.setId(id);
        serveReserveDo.setStatus(reserveStatusEnum.getValue());
        this.serveReserveDao.updateServeStatus(serveReserveDo);
    }

    
}
